package rbacControllers

import (
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"lazyops_go/models"
	"lazyops_go/tools"
)

type RoleController struct {
	beego.Controller
}

func (c *RoleController) IndexRole() {
	cur_page,_ := c.GetInt(":page",1)
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	role_list :=models.QueryRoleAll()
	data_list := models.QueryRolePage(cur_page)
	page:=tools.CreatePaging(cur_page,12,len(role_list))
	menu_list := models.QueryMenuAll()

	c.Data["page"] = page
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["menu_list"] = menu_list
	c.Data["role_list"] = data_list
	c.Data["label"] = "角色分组"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "rbac/rolejs.html"
	c.LayoutSections["HtmlHead"] = "rbac/rolecss.html"
	c.TplName = "rbac/role.html"
}


func (c *RoleController) CreateRole() {
	rolename := c.GetString("rolename")
	rolemsg := c.GetString("rolemsg")

	role := models.Role{}
	role.RoleName = rolename
	role.RoleMsg = rolemsg
	models.AddRole(role)
	c.Data["json"] = "add success"
	c.ServeJSON()
}


//通过ID获取角色信息
func (c *RoleController) GetRoleById(){
	role_id,_ := c.GetInt("role_id",0)
	role_obj := models.QueryRoleOne(role_id)
	c.Data["json"] = role_obj
	c.ServeJSON()
}

//修改用户信息
func (c *RoleController) EditRole()  {
	role_id,_:= c.GetInt("role_id", 0)
	rolename := c.GetString("rolename")
	rolemsg := c.GetString("rolemsg")

	//实例 Role 结构体
	new_role := models.Role{}
	new_role.RoleName = rolename
	new_role.RoleMsg = rolemsg
	models.UpdateRole(role_id,new_role)
	c.Data["json"] = "修改成功"
	c.ServeJSON()
}


//删除角色
func (c *RoleController) DelRole() {
	role_id,_:= c.GetInt("role_id", 0)
	fmt.Println(role_id)
	models.DeleteRole(role_id)
	c.Data["json"] = "删除成功"
	c.ServeJSON()
}

//搜索
func (c *RoleController) SearchRole() {

	key := c.Ctx.Input.Param(":key")
	role_list := models.QueryRoleSearch(key)
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")

	page:=tools.CreatePaging(1,len(role_list),len(role_list))
	c.Data["page"] = page
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["key"] = key
	c.Data["role_list"] = role_list
	c.Data["label"] = "角色分组"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "rbac/rolejs.html"
	c.TplName = "rbac/role.html"
}


//通过ID获取角色信息
func (c *RoleController) GetPerms(){
	role_id,_ := c.GetInt("role_id",0)
	role_obj := models.GetRolePerms(role_id)
	c.Data["json"] = role_obj
	c.ServeJSON()
}

func (c *RoleController)  EditRolePerms() {
	role_id,_ := c.GetInt("role_id",0)
	perms_ids := c.GetString("perms_ids")
	//定义role_list 切片（列表）
	var perms_id_list []int

	err := json.Unmarshal([]byte(perms_ids), &perms_id_list)
	if err != nil {
		fmt.Println("json 转换失败")
	}

	//实例Role结构体
	role_obj := models.Role{}
	var perms_list [] models.Menu

	for i,_ := range perms_id_list {
		//把string 转成int
		id:= perms_id_list[i]
		//获取role对象
		menu := models.GetMenuId(id)
		perms_list = append(perms_list,menu)
	}

	old_role := models.QueryRoleOne(role_id)
	role_obj.RoleName = old_role.RoleName
	role_obj.RoleMsg = old_role.RoleMsg
	role_obj.Perms = perms_list

	models.UpdateRolePerms(role_id,role_obj)
	c.Data["json"] = "授权成功"
	c.ServeJSON()
}



func (c *RoleController) GetPfm(){
	//通过ID获取角色授权平台信息
	role_id,_ := c.GetInt("role_id",0)
	role_pfm_ids := models.GetRolePfm(role_id)
	prj_obj := models.QueryPrjAll()
	var ztree_data_list []models.ZtreeDate
	for _, v := range prj_obj {
		pfm_id := v.ID
		pfm_obj := models.GetPlatfmList(pfm_id)
		var ztr_head models.ZtreeDate
		ztr_head.Id = pfm_id
		ztr_head.Pid = 0
		ztr_head.Name = v.PrjName
		ztr_head.Open = true
		for _, vv := range pfm_obj {
			var ztree models.ZtreeDate
			ztree.Id = vv.ID*1000
			ztree.Pid = pfm_id
			ztree.Name = fmt.Sprintf(vv.PlatfmName)
			ztree.Open = false
			check := models.IsExitItem(vv.ID,role_pfm_ids)
			if check {
				ztree.Checked = true
				ztr_head.Checked = true
			}
			ztree_data_list = append(ztree_data_list, ztree)
		}
		ztree_data_list = append(ztree_data_list, ztr_head)
	}
	c.Data["json"] = ztree_data_list
	c.ServeJSON()
}

func (c *RoleController)  EditRolePfm() {
	role_id,_ := c.GetInt("role_id",0)
	pfm_ids_json := c.GetString("pfm_ids")
	//定义role_list 切片（列表）
	var pfm_id_list []int

	err := json.Unmarshal([]byte(pfm_ids_json), &pfm_id_list)
	if err != nil {
		fmt.Println("json 转换失败")
	}

	//实例Role结构体
	role_obj := models.Role{}
	var platfm_list [] models.ItemPlatfm

	for _,v := range pfm_id_list {
		obj := models.QueryPlatfmOne(v)
		platfm_list = append(platfm_list,obj)
	}
	old_role := models.QueryRoleOne(role_id)
	role_obj.RoleName = old_role.RoleName
	role_obj.RoleMsg = old_role.RoleMsg
	role_obj.PlatForm = platfm_list

	models.UpdateRolePfm(role_id,role_obj)
	c.Data["json"] = "授权成功"
	c.ServeJSON()
}

